﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Runtime.Serialization;

namespace DependencyAnalysis.Serialization
{
    [DataContract]
    [Serializable]
    [KnownType(typeof(List<DataContractExpandoObject>))]
    public class DataContractExpandoObject : DynamicObject, IReportObject
    {
        private Dictionary<string, object> _properties = new Dictionary<string, object>();

        [DataMember(Order = 1)]
        public Dictionary<string, object> Properties
        {
            get { return _properties; }
            set { _properties = value; }
        }

        [DataMember(Order = 2)]
        public string Name { get; set; }

        [DataMember(Order = 3)]
        public string TypeName { get; set; }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            _properties.TryGetValue(binder.Name, out result);
            return true;
        }

        public void Set(string key, object value)
        {
            _properties[key] = value;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            _properties[binder.Name] = value;
            return true;
        }
    }
}
